package com.cc.tojsonscheam.javadoc;

import com.alibaba.fastjson.JSON;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.FieldDoc;
import com.sun.javadoc.RootDoc;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;


/**
 * detail: 读取 JavaDoc 转换为Json-Schema
 * <p>
 * 不支持嵌套
 *
 * @author cc
 * @date 2022/8/30
 */

class JavaDocMain {

    public static void main(String[] args) {
        // 文件路径
        final String path = "D:\\IdeaProjects\\1\\to-jsonSchema\\src\\main\\java\\com\\cc\\tojsonscheam\\victools\\";
        List<File> list = new ArrayList<>();
        //getDirectory("D:\\wrok\\git_space\\ngs_bid\\src\\main\\java\\io\\bm", list);
        // 类名
        final String className = "AfterSaleSkuDTO.java";
        // 执行参数
        final String[] executeParams = JavaDocReader.getExecuteParams(true, path, className);
        Map<String, Object> root = new HashMap<>();
        Map<String, Object> child = new HashMap<>();
        // 读取文档
        String javaDcoData = JavaDocReader.readDoc(new JavaDocReader.Callback() {
            @Override
            public String callback(
                    String path,
                    String className,
                    RootDoc rootDoc,
                    ClassDoc[] classDocs) {
                StringBuilder builder = new StringBuilder();
                builder.append("\n\n");
                builder.append("\n===============");
                builder.append("\n= 读取 JavaDoc =");
                builder.append("\n===============");
                builder.append("\n");
                // 拼接信息
                builder.append("\n文件路径: ").append(path).append(className)
                        .append("\n文件名字: ").append(className)
                        .append("\n执行参数: ").append(Arrays.toString(executeParams));

                if (classDocs != null) {
                    StringBuilder classBuilder = new StringBuilder();
                    // 循环 Class Doc 信息
                    for (ClassDoc classDoc : classDocs) {
                        classBuilder.append("\n\n");
                        classBuilder.append("\n= ").append(classDoc.name()).append(" =\n");
                        // 包名.类名
                        classBuilder.append("\n包名.类名: ").append(classDoc);
                        // 类注释
                        classBuilder.append("\n类注释: ").append(classDoc.commentText());
                        FieldDoc[] fields = classDoc.fields();

                        // 循环读取方法信息
                        for (FieldDoc fieldDoc : fields) {
                            Map<String, Object> node = new HashMap<>();
                            node.put("description", fieldDoc.commentText());
                            String typeName = fieldDoc.type().typeName();
                            if (typeName.equalsIgnoreCase("LocalDateTime")) {
                                typeName = "string";
                            } else if (typeName.equalsIgnoreCase("Long")) {
                                typeName = "number";
                            } else if (typeName.equalsIgnoreCase("List")) {
                                typeName = "array";
                            } else {
                                typeName = "string";
                            }
                            node.put("type", typeName.toLowerCase());
                            child.put(fieldDoc.name(), node);
                        }
                    }

                    root.put("type", "object");
                    root.put("properties", child);
                    classBuilder.append("\n\n生成的Json：\n\n").append(JSON.toJSONString(root));
                    // 保存类信息
                    builder.append(classBuilder);
                }
                builder.append("\n\n");
                builder.append("\n===================");
                builder.append("\n= 读取 JavaDoc 结束 =");
                builder.append("\n===================");
                builder.append("\n");

                // 返回文档信息
                return builder.toString();
            }

            @Override
            public void error(Exception e) {
                System.out.println(e);
            }
        }, path, className, executeParams);
        // 打印文档信息
        System.out.println(javaDcoData);
    }

    /**
     * 获取entity所有的文件夹
     *
     * @param path     路径
     * @param pathList 路径列表
     * @return {@link List}<{@link File}>
     */
    private static List<File> getDirectory(String path, List<File> pathList) {
        File file = new File(path);
        File[] files = file.listFiles();

        List<File> collect = Arrays.stream(files).filter(v -> "entity".equalsIgnoreCase(v.getName())).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            pathList.addAll(collect);
        }
        for (File f : files) {
            if (f.isDirectory()) {
                getDirectory(f.getAbsolutePath(), pathList);
            }
        }
        return pathList;
    }
}
